import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class AngularInterceptor implements HttpInterceptor {

    constructor() {
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        // 追加表示 Ajax 访问的头部信息
        const cloned = req.clone({
            headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')
        });

        return next.handle(cloned);
    }
}
